<template>
  <div class="product-detail-container" v-if="product">
    <el-card>
      <div class="product-main-info">
        <div class="product-images">
          <div class="main-image">
            <img :src="product.image" alt="商品主图">
          </div>
          <!-- 如果有多张图片，可以在这里添加缩略图展示 -->
        </div>
        
        <div class="product-details">
          <h1 class="product-title">{{ product.title }}</h1>
          <div class="product-price">¥{{ product.price }}</div>
          <div class="product-meta">
            <span>分类：{{ product.categoryName }}</span>
            <span>发布时间：{{ product.createdAt }}</span>
            <span>浏览次数：{{ product.viewCount || 0 }}</span>
          </div>
          
          <div class="seller-info">
            <h3>卖家信息</h3>
            <div class="seller-details">
              <el-avatar :size="60" :src="product.sellerAvatar || 'https://via.placeholder.com/60'" />
              <div class="seller-text">
                <div class="seller-name">{{ product.sellerName }}</div>
                <div class="seller-rating">信誉评分：⭐⭐⭐⭐⭐</div>
              </div>
              <el-button type="primary" @click="contactSeller">联系卖家</el-button>
            </div>
          </div>
          
          <div class="action-buttons">
            <el-button type="primary" size="large" @click="addToCart">
              <el-icon><i-ep-shopping-cart /></el-icon>
              加入购物车
            </el-button>
            <el-button type="success" size="large" @click="buyNow">
              <el-icon><i-ep-aim /></el-icon>
              立即购买
            </el-button>
            <el-button type="info" size="large" @click="favoriteProduct">
              <el-icon><i-ep-star /></el-icon>
              {{ isFavorite ? '已收藏' : '收藏' }}
            </el-button>
          </div>
        </div>
      </div>
    </el-card>

    <div class="product-tabs">
      <el-tabs v-model="activeTab">
        <el-tab-pane label="商品详情" name="details">
          <div class="product-description">
            <h2>商品描述</h2>
            <div class="description-content">
              {{ product.description }}
            </div>
          </div>
          
          <div class="product-params">
            <h2>商品参数</h2>
            <el-table :data="productParams">
              <el-table-column prop="name" label="参数名" width="120" />
              <el-table-column prop="value" label="参数值" />
            </el-table>
          </div>
        </el-tab-pane>
        
        <el-tab-pane label="用户评价" name="reviews">
          <div class="reviews-section">
            <div class="reviews-header">
              <h2>用户评价 ({{ reviews.length }})</h2>
              <el-button type="primary" @click="writeReview" v-if="userStore.isLoggedIn">写评价</el-button>
            </div>
            
            <div class="reviews-filter">
              <el-radio-group v-model="reviewFilter">
                <el-radio-button label="all">全部</el-radio-button>
                <el-radio-button label="good">好评</el-radio-button>
                <el-radio-button label="neutral">中评</el-radio-button>
                <el-radio-button label="bad">差评</el-radio-button>
              </el-radio-group>
            </div>
            
            <div class="review-list">
              <div v-for="review in filteredReviews" :key="review.id" class="review-item">
                <div class="review-header">
                  <el-avatar :size="40" :src="review.userAvatar || 'https://via.placeholder.com/40'" />
                  <div class="review-user-info">
                    <div class="review-user-name">{{ review.userName }}</div>
                    <div class="review-rating">
                      <span v-for="i in 5" :key="i" class="star">
                        {{ i <= review.rating ? '⭐' : '☆' }}
                      </span>
                    </div>
                  </div>
                  <div class="review-time">{{ review.createdAt }}</div>
                </div>
                <div class="review-content">{{ review.content }}</div>
                <div class="review-images" v-if="review.images && review.images.length">
                  <img v-for="(img, index) in review.images" :key="index" :src="img" alt="评价图片">
                </div>
              </div>
            </div>
            
            <div v-if="reviews.length === 0" class="empty-reviews">
              <el-empty description="暂无评价" />
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>

    <div class="recommended-products">
      <h2>推荐商品</h2>
      <div class="product-grid">
        <div v-for="item in recommendedProducts" :key="item.id" class="product-card">
          <router-link :to="`/product/${item.id}`">
            <div class="product-image">
              <img :src="item.image" alt="商品图片">
            </div>
            <div class="product-info">
              <h3 class="product-title">{{ item.title }}</h3>
              <div class="product-price">¥{{ item.price }}</div>
            </div>
          </router-link>
        </div>
      </div>
    </div>
  </div>
  
  <!-- 加载状态 -->
  <div v-else class="loading">
    <el-skeleton :rows="10" animated />
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useProductStore, useCartStore, useUserStore } from '../../store'

const route = useRoute()
const router = useRouter()
const productStore = useProductStore()
const cartStore = useCartStore()
const userStore = useUserStore()

const activeTab = ref('details')
const isFavorite = ref(false)
const reviewFilter = ref('all')
const product = ref(null)

// 模拟商品详情数据
const mockProduct = {
  id: 1,
  title: 'iPhone 13 128GB 蓝色 95新',
  price: 3999,
  image: 'https://via.placeholder.com/600x600',
  categoryId: 1,
  categoryName: '数码产品',
  sellerId: 101,
  sellerName: '小明',
  sellerAvatar: 'https://via.placeholder.com/60',
  description: '这是一台iPhone 13 128GB 蓝色，购买于2023年5月，使用不到一年，成色95新，所有功能正常，没有任何磕碰和划痕。配件包括原装充电器和数据线，盒子还在。因为换新机所以出售，价格可小刀。',
  createdAt: '2024-10-20',
  viewCount: 125,
  status: 'available'
}

// 商品参数
const productParams = [
  { name: '品牌', value: 'Apple' },
  { name: '型号', value: 'iPhone 13' },
  { name: '存储容量', value: '128GB' },
  { name: '颜色', value: '蓝色' },
  { name: '购买日期', value: '2023-05' },
  { name: '成色', value: '95新' },
  { name: '配件', value: '充电器、数据线、包装盒' },
  { name: '保修情况', value: '已过保' }
]

// 模拟评价数据
const reviews = [
  {
    id: 1,
    userName: '小李',
    userAvatar: 'https://via.placeholder.com/40',
    rating: 5,
    content: '手机很新，和描述的一样，卖家也很热情，值得推荐！',
    images: ['https://via.placeholder.com/100x100'],
    createdAt: '2024-10-21'
  },
  {
    id: 2,
    userName: '小张',
    userAvatar: 'https://via.placeholder.com/40',
    rating: 4,
    content: '总体不错，就是快递慢了一点。',
    images: [],
    createdAt: '2024-10-20'
  }
]

// 推荐商品
const recommendedProducts = [
  {
    id: 6,
    title: '索尼耳机 WH-1000XM4',
    price: 1299,
    image: 'https://via.placeholder.com/300x300'
  },
  {
    id: 7,
    title: 'MacBook Pro 2022 14寸',
    price: 12999,
    image: 'https://via.placeholder.com/300x300'
  },
  {
    id: 12,
    title: 'U盘 128GB 高速',
    price: 69,
    image: 'https://via.placeholder.com/300x300'
  }
]

// 过滤后的评价
const filteredReviews = computed(() => {
  if (reviewFilter.value === 'all') return reviews
  
  switch (reviewFilter.value) {
    case 'good':
      return reviews.filter(r => r.rating >= 4)
    case 'neutral':
      return reviews.filter(r => r.rating === 3)
    case 'bad':
      return reviews.filter(r => r.rating <= 2)
    default:
      return reviews
  }
})

onMounted(async () => {
  const { id } = route.params
  // 在实际应用中，这里应该调用API获取商品详情
  // await productStore.getProductById(id)
  // product.value = productStore.currentProduct
  
  // 使用模拟数据
  product.value = mockProduct
})

const addToCart = () => {
  if (!userStore.isLoggedIn) {
    ElMessage.warning('请先登录')
    router.push('/login')
    return
  }
  
  cartStore.addToCart(product.value)
  ElMessage.success('已加入购物车')
}

const buyNow = () => {
  if (!userStore.isLoggedIn) {
    ElMessage.warning('请先登录')
    router.push('/login')
    return
  }
  
  // 将当前商品加入购物车，然后跳转到确认订单页面
  cartStore.addToCart(product.value)
  router.push('/order/confirm')
}

const favoriteProduct = () => {
  if (!userStore.isLoggedIn) {
    ElMessage.warning('请先登录')
    router.push('/login')
    return
  }
  
  isFavorite.value = !isFavorite.value
  ElMessage.success(isFavorite.value ? '收藏成功' : '取消收藏')
}

const contactSeller = () => {
  ElMessage.info('正在跳转到聊天界面...')
  // 实际应用中应该跳转到聊天页面
}

const writeReview = () => {
  ElMessage.info('请先购买商品后再评价')
  // 实际应用中应该检查用户是否购买过该商品
}
</script>

<style scoped>
.product-detail-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

:deep(.el-card) {
  border-radius: 16px;
  border: none;
  box-shadow: 0 5px 30px rgba(0, 0, 0, 0.08);
  overflow: hidden;
}

.product-main-info {
  display: flex;
  gap: 50px;
  padding: 30px;
}

.product-images {
  flex: 1;
  max-width: 550px;
}

.main-image {
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  transition: transform 0.5s ease;
}

.main-image:hover {
  transform: scale(1.02);
}

.main-image img {
  width: 100%;
  height: 450px;
  object-fit: cover;
}

.product-details {
  flex: 1;
  min-width: 300px;
}

.product-title {
  font-size: 32px;
  margin-bottom: 25px;
  color: #333;
  line-height: 1.4;
}

.product-price {
  font-size: 36px;
  font-weight: bold;
  color: #ff4d4f;
  margin-bottom: 25px;
  display: inline-block;
  padding: 10px 20px;
  background: linear-gradient(135deg, #fff0f0 0%, #fff5f5 100%);
  border-radius: 12px;
}

.product-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 25px;
  margin-bottom: 35px;
  padding: 20px;
  background-color: #f9f9ff;
  border-radius: 12px;
  color: #666;
  font-size: 15px;
}

.product-meta span {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
}

.seller-info {
  border-top: 1px solid #f0f0f0;
  padding-top: 25px;
  margin-bottom: 35px;
}

.seller-info h3 {
  margin-bottom: 20px;
  font-size: 20px;
  color: #333;
  font-weight: 600;
}

.seller-details {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 20px;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 3px 15px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.seller-details:hover {
  box-shadow: 0 5px 20px rgba(102, 126, 234, 0.1);
  transform: translateY(-3px);
}

:deep(.el-avatar) {
  border: 3px solid #667eea;
}

.seller-text {
  flex: 1;
}

.seller-name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 8px;
  color: #333;
}

.seller-rating {
  color: #ffb400;
  font-size: 15px;
}

.action-buttons {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

/* 覆盖Element Plus按钮样式 */
:deep(.el-button) {
  border-radius: 12px;
  padding: 12px 30px;
  font-size: 16px;
  font-weight: 600;
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

:deep(.el-button--primary),
:deep(.el-button--success),
:deep(.el-button--info) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: transparent;
  box-shadow: 0 5px 20px rgba(102, 126, 234, 0.3);
}

:deep(.el-button--primary:hover),
:deep(.el-button--success:hover),
:deep(.el-button--info:hover) {
  background: linear-gradient(135deg, #5a67d8 0%, #6b46c1 100%);
  transform: translateY(-3px);
  box-shadow: 0 8px 30px rgba(102, 126, 234, 0.4);
}

:deep(.el-button--success) {
  background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
  box-shadow: 0 5px 20px rgba(72, 187, 120, 0.3);
}

:deep(.el-button--success:hover) {
  background: linear-gradient(135deg, #38a169 0%, #2f855a 100%);
  box-shadow: 0 8px 30px rgba(72, 187, 120, 0.4);
}

:deep(.el-button--info) {
  background: linear-gradient(135deg, #3182ce 0%, #2c5282 100%);
  box-shadow: 0 5px 20px rgba(49, 130, 206, 0.3);
}

:deep(.el-button--info:hover) {
  background: linear-gradient(135deg, #2c5282 0%, #2a4365 100%);
  box-shadow: 0 8px 30px rgba(49, 130, 206, 0.4);
}

.product-tabs {
  margin-top: 40px;
  margin-bottom: 50px;
}

:deep(.el-tabs__nav-wrap) {
  padding: 0 10px;
}

:deep(.el-tabs__nav) {
  padding: 0;
}

:deep(.el-tabs__item) {
  padding: 15px 30px;
  font-size: 18px;
  font-weight: 600;
  color: #666;
  position: relative;
  transition: all 0.3s ease;
}

:deep(.el-tabs__item:hover) {
  color: #667eea;
}

:deep(.el-tabs__item.is-active) {
  color: #667eea;
}

:deep(.el-tabs__active-bar) {
  height: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 2px;
  bottom: 0;
}

.product-description,
.product-params {
  padding: 30px;
  background-color: #ffffff;
  border-radius: 16px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
  margin-bottom: 30px;
}

.product-description h2,
.product-params h2 {
  margin-bottom: 25px;
  font-size: 24px;
  color: #333;
  position: relative;
  padding-left: 20px;
}

.product-description h2::before,
.product-params h2::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 8px;
  height: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 4px;
}

.description-content {
  line-height: 1.8;
  color: #333;
  white-space: pre-wrap;
  font-size: 16px;
  padding: 20px;
  background-color: #f9f9ff;
  border-radius: 12px;
}

:deep(.el-table) {
  border-radius: 12px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #f8f9ff;
  font-weight: 600;
  color: #333;
}

.reviews-section {
  padding: 30px;
  background-color: #ffffff;
  border-radius: 16px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
}

.reviews-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.reviews-header h2 {
  font-size: 24px;
  color: #333;
  font-weight: 600;
  position: relative;
  padding-left: 20px;
}

.reviews-header h2::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 8px;
  height: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 4px;
}

.reviews-filter {
  margin-bottom: 30px;
}

:deep(.el-radio-button__inner) {
  border-radius: 20px;
  padding: 8px 20px;
  transition: all 0.3s ease;
}

:deep(.el-radio-button__orig-radio:checked + .el-radio-button__inner) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: #667eea;
}

.review-list {
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.review-item {
  padding: 25px;
  border: none;
  border-radius: 16px;
  background-color: #f8f9ff;
  box-shadow: 0 3px 15px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.review-item:hover {
  box-shadow: 0 8px 30px rgba(102, 126, 234, 0.1);
  transform: translateY(-3px);
}

.review-header {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 20px;
}

.review-user-info {
  flex: 1;
}

.review-user-name {
  font-weight: bold;
  font-size: 16px;
  margin-bottom: 8px;
  color: #333;
}

.review-rating .star {
  color: #ffb400;
  font-size: 18px;
}

.review-time {
  color: #999;
  font-size: 14px;
}

.review-content {
  line-height: 1.7;
  margin-bottom: 20px;
  font-size: 16px;
  color: #666;
}

.review-images img {
  width: 120px;
  height: 120px;
  margin-right: 15px;
  border-radius: 10px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.review-images img:hover {
  transform: scale(1.05);
}

.empty-reviews {
  padding: 60px 0;
  text-align: center;
}

:deep(.el-empty) {
  color: #999;
}

.recommended-products {
  margin-top: 50px;
  padding: 30px;
  background-color: #ffffff;
  border-radius: 16px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
}

.recommended-products h2 {
  margin-bottom: 30px;
  font-size: 24px;
  color: #333;
  font-weight: 600;
  position: relative;
  padding-left: 20px;
}

.recommended-products h2::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 8px;
  height: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 4px;
}

.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 30px;
}

.product-card {
  border: none;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.product-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 15px 30px rgba(102, 126, 234, 0.15);
}

.product-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.product-card:hover::before {
  opacity: 1;
}

.product-image {
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.product-card:hover .product-image img {
  transform: scale(1.05);
}

.product-info {
  padding: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.product-info .product-title {
  font-size: 18px;
  margin-bottom: 15px;
  color: #333;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
  flex: 1;
}

.product-info .product-price {
  font-size: 22px;
  font-weight: bold;
  color: #ff4d4f;
  padding: 5px 10px;
  background: none;
  border-radius: 0;
  margin-bottom: 0;
}

.loading {
  padding: 40px;
  text-align: center;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .product-detail-container {
    padding: 30px 20px;
  }
  
  .product-main-info {
    gap: 30px;
    padding: 20px;
  }
  
  .main-image img {
    height: 400px;
  }
  
  .product-title {
    font-size: 28px;
  }
}

@media (max-width: 768px) {
  .product-detail-container {
    padding: 20px 15px;
  }
  
  .product-main-info {
    flex-direction: column;
    padding: 15px;
  }
  
  .main-image img {
    height: 300px;
  }
  
  .product-title {
    font-size: 24px;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .product-meta {
    gap: 15px;
  }
  
  .reviews-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 20px;
  }
  
  .product-grid {
    gap: 20px;
  }
  
  .review-header {
    flex-wrap: wrap;
  }
}

@media (max-width: 480px) {
  .product-description,
  .product-params,
  .reviews-section,
  .recommended-products {
    padding: 20px;
  }
  
  .product-price {
    font-size: 30px;
  }
  
  .product-grid {
    grid-template-columns: 1fr;
  }
  
  :deep(.el-tabs__item) {
    padding: 12px 15px;
    font-size: 16px;
  }
}
</style>